package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IHomeSiteCommendCompanyDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.StringUtils;
import com.wsj.util.tool.Tool;
import com.wsj.web.vo.homeSite.HomeSiteCommendCompany;

public class HomeSiteCommendCompanyDaoImpl extends HibernateDaoSupport implements IHomeSiteCommendCompanyDao{

	public void del(HomeSiteCommendCompany homeSiteCommendCompany) throws DataAccessException {
		if(homeSiteCommendCompany != null){
			this.getHibernateTemplate().delete(homeSiteCommendCompany);
		}
	}

	public void saveOrUpdate(HomeSiteCommendCompany homeSiteCommendCompany) throws DataAccessException {
		if(homeSiteCommendCompany != null){
			this.getHibernateTemplate().saveOrUpdate(homeSiteCommendCompany);
		}
	}
	
	public HomeSiteCommendCompany loadById(Integer id) throws DataAccessException {
		if(id != null && !new Integer(0).equals(id)){
			return (HomeSiteCommendCompany) this.getHibernateTemplate().get(HomeSiteCommendCompany.class, id);
		}
		return null;
	}

	/**
	 * 第一部分 通过 comType =’zst’ , isCommend=1 , isHot=1 , isPic!=1 按照排序字段排序
	 * 中间两张图片 通过 comType =’zst’ , isCommend=1 , isHot=1 , isPic==1  按照排序字段排序 
	 * 第三部分 通过 comType =’zst’ , isCommend=1 , isHot!=1 , isPic!=1 按照排序字段排序. 
	 * 
	 * 2008.11.07 GBX join 推荐企业显示位置:String bmCommendCompanySiteStr
	 * */
	@SuppressWarnings("unchecked")
	public List<HomeSiteCommendCompany> loadList(String comName, String comType, Short isHot, Short isCommend, 
			Short isPic,String bmCommendCompanySiteStr,Integer top) throws DataAccessException {
		StringBuffer stringBuffer = new StringBuffer(" from HomeSiteCommendCompany as h where 1 = 1");
		//	 企业名称
		if(StringUtils.isNotNull(comName)){
			stringBuffer.append(" and h.comName like :comName ");
		}
		//   企业类型：
		if(StringUtils.isNotNull(comType)){
			stringBuffer.append(" and h.comType = :comType ");
		}
		//  是否热点
		if(isHot != null && !new Short("-1").equals(isHot)){
			stringBuffer.append(" and h.isHot = :isHot ");
		}
		//  是否推荐
		if(isCommend != null && !new Short("-1").equals(isCommend)){
			stringBuffer.append(" and h.isCommend = :isCommend ");
		}
		//  是否有图片
		if(isPic != null && !new Short("-1").equals(isPic)){
			stringBuffer.append(" and h.isPic = :isPic");
		}
		//  推荐企业显示位置
		if(StringUtils.isNotNull(bmCommendCompanySiteStr)){
			stringBuffer.append(" and h.bmCommendCompanySite.siteName like :bmCommendCompanySiteStr ");
		}
		stringBuffer.append(" order by h.sequence desc,h.id desc ");
		Query query = this.getSession().createQuery(stringBuffer.toString());
		
		if(StringUtils.isNotNull(comName)){
			query.setString("comName", "%"+comName+"%");
		}
		if(StringUtils.isNotNull(comType)){
			query.setString("comType", comType);
		}
		if(isHot != null && !new Short("-1").equals(isHot)){
			query.setShort("isHot", isHot);
		}
		if(isCommend != null && !new Short("-1").equals(isCommend)){
			query.setShort("isCommend", isCommend);
		}
		if(isPic != null && !new Short("-1").equals(isPic)){
			query.setShort("isPic", isPic);
		}
		if(StringUtils.isNotNull(bmCommendCompanySiteStr)){
			query.setString("bmCommendCompanySiteStr", "%"+bmCommendCompanySiteStr+"%");
		}
		
		if(top != null && !new Integer(0).equals(top)){
			query.setMaxResults(top);
		}
		
		List list = query.list();
		if(StringUtils.isNotNull(list)){
			return list;
		}
		return null;
	}

	
	public PageResultSet loadPage(String comName, String comType, Short isHot, Short isCommend, Short isPic, 
			String bmCommendCompanySiteStr, Integer page, Integer pageSize) throws DataAccessException {
		// 初始化
		PageResultSet pageResultSet = new PageResultSet();
		PerfectPageResultSet pageResult = new PerfectPageResultSet();
		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),true);
		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer(" from HomeSiteCommendCompany as h where 1 = 1");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(page, pageSize);
		
        // 参数
        List<Comparable> list = new ArrayList<Comparable>();
        //   	 企业名称
		if(StringUtils.isNotNull(comName)){
			stringBuffer.append(" and h.comName like ? ");
			list.add("%"+comName+"%");
		}
		//   企业类型：
		if(StringUtils.isNotNull(comType) && !"-1".equals(comType)){
			if("zst".equals(comType)){
				stringBuffer.append(" and h.comType = 'zst' ");
			}
			if("wgt".equals(comType)){
				stringBuffer.append(" and h.comType = 'wgt' ");
			}
		}
		//  是否热点
		if(isHot != null && !new Short("-1").equals(isHot)){
			stringBuffer.append(" and h.isHot = ? ");
			list.add(isHot);
		}
		//  是否推荐
		if(isCommend != null && !new Short("-1").equals(isCommend)){
			stringBuffer.append(" and h.isCommend = ? ");
			list.add(isCommend);
		}
		//  是否有图片
		if(new Short("0").equals(isPic)){
			stringBuffer.append(" and h.isPic = 0 ");
		}
		if(new Short("1").equals(isPic)){
			stringBuffer.append(" and h.isPic = 1 ");
		}
		if(StringUtils.isNotNull(bmCommendCompanySiteStr)){
			stringBuffer.append(" and h.bmCommendCompanySite.siteName = ?");
			list.add(bmCommendCompanySiteStr);
		}
        
		stringBuffer.append(" order by h.sequence desc, h.id desc");
			
		String[] strParams = Tool.toArray(list);
		pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),session, strParams);
		return pageResultSet;
	}

	

}
